<?php namespace App\Http\Controllers\Api\Admin;

use App\AboutUs;
use App\Achievement;
use App\Admin;
use App\ApplicationField;
use App\Banner;
use App\BooksChapter;
use App\BooksInfo;
use App\CharitableActivities;
use App\Classify;
use App\Comment;
use App\EnAboutUs;
use App\EnAchievement;
use App\EnApplicationField;
use App\EnBanner;
use App\EnCharitableActivities;
use App\EnComment;
use App\EnEnvironmentalProtectionKnowledge;
use App\EnFooterMenu;
use App\EnGoodsProduct;
use App\EnHumanResources;
use App\EnNews;
use App\EnPhilanthropy;
use App\EnProjectCases;
use App\EnSysInfo;
use App\EnvironmentalProtectionKnowledge;
use App\FooterMenu;
use App\GoodsProduct;
use App\Http\Controllers\Controller;
use App\HumanResources;
use App\OnlineMessage;
use App\Philanthropy;
use App\ProjectCases;
use App\RechargePackage;
use App\SysInfo;
use App\User;
use App\VipRule;
use Auth;
use EasyWeChat\Kernel\Messages\News;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionNotValidBundle\ExtensionNotValidBundle;

class SystemController extends Controller
{
    public function setBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title       = $request->get('title');
        $desc        = $request->get('desc');
        $linkUrl     = $request->get('link_url');
        $buttonTitle = $request->get('button_title');
        $sort        = $request->get('sort');
        $imgId       = $request->get('img_id');
        $type        = !is_null($request->get('type')) ? $request->get('type') : '';
        $id          = !is_null($request->get('id'))?$request->get('id'):0;

        if($type == 'banner'){
            $thatALLBanner = Banner::whereId(trim($id))->first();

            if(!$thatALLBanner)
                $thatALLBanner = new Banner();

            if(!is_null($title))$thatALLBanner->title = $title;
            if(!is_null($desc))$thatALLBanner->desc = $desc;
            if(!is_null($linkUrl))$thatALLBanner->link_url = $linkUrl;
            if(!is_null($buttonTitle))$thatALLBanner->button_title = $buttonTitle;
            if(!is_null($sort))$thatALLBanner->sort = $sort;
            if(!is_null($imgId))$thatALLBanner->img_id = $imgId;
            $thatALLBanner->save();
        }elseif($type == 'en_banner'){
            $thatALLBanner = EnBanner::whereId(trim($id))->first();

            if(!$thatALLBanner)
                $thatALLBanner = new EnBanner();

            if(!is_null($title))$thatALLBanner->title = $title;
            if(!is_null($desc))$thatALLBanner->desc = $desc;
            if(!is_null($linkUrl))$thatALLBanner->link_url = $linkUrl;
            if(!is_null($buttonTitle))$thatALLBanner->button_title = $buttonTitle;
            if(!is_null($sort))$thatALLBanner->sort = $sort;
            if(!is_null($imgId))$thatALLBanner->img_id = $imgId;
            $thatALLBanner->save();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$thatALLBanner]);
    }

    public function delBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $type    = !is_null($request->get('type')) ? $request->get('type') : '';

        if($type == 'banner'){
            /** @var Banner $thatBanner */
            $thatBanner = Banner::whereId($id)->first();
        }elseif($type == 'en_banner'){
            /** @var EnBanner $thatBanner */
            $thatBanner = EnBanner::whereId($id)->first();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        if(!$thatBanner)
            return response()->json(["level"=>"error","message"=>"该banner不存在！"]);

        $thatBanner->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatBanner]);
    }

    public function setAdmin(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $username = $request->get('username');
        $password = $request->get('password');
        $repass   = $request->get('repass');

        if($password != $repass)
            return response()->json(['level'=>'error','message' => '两次密码不一致！']);

        /** @var Admin $hasUser */
        $hasUser = Admin::whereUsername($username)->first();
        if($hasUser){
            return response()->json(['level'=>'error','message' => '当前管理员账号已存在']);
        }

        $thatAdmin = new Admin();
        $thatAdmin->username = $username;
        $thatAdmin->password = bcrypt($password);
        $thatAdmin->status = 2;
        $thatAdmin->save();

        return response()->json(['level'=>'success','message'=>'添加成功','data'=>$thatAdmin]);
    }

    public function delAdmin(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Admin $thatAdmin */
        $thatAdmin = Admin::whereId($id)->first();

        if(!$thatAdmin)
            return response()->json(["level"=>"error","message"=>"该管理员不存在！"]);

        if($thatAdmin->status == 1)
            return response()->json(["level"=>"error","message"=>"该管理员不能删除！"]);


        $thatAdmin->forceDelete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatAdmin]);
    }

    public function addClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $name        = $request->get("name");
        $sectionName = $request->get("section_name");
        $desc        = $request->get("desc");
        $sort        = $request->get('sort');
        $id          = !is_null($request->get('id'))?$request->get('id'):0;
        $imgId       = !is_null($request->get('img_id'))?$request->get('img_id'):0;

        $newClassify = Classify::whereId(trim($id))->first();
        if(!$newClassify)
            return response()->json(['level' => 'error','message' => '该数据不存在']);

        if(!is_null($sectionName))$newClassify->section_name = $sectionName;
        if(!is_null($name))$newClassify->name                = $name;
        if(!is_null($desc))$newClassify->desc                = $desc;
        if(!is_null($sort))$newClassify->sort                = $sort;
        $newClassify->img_id                                 = $imgId;
        $newClassify->save();

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newClassify]);
    }

    public function delClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $thatClassify =Classify::whereId($id)->first();

        if(!$thatClassify)
            return response()->json(["level"=>"error","message"=>"该分类不存在！"]);

        $thatClassify->delete();

        return response()->json(['level' => 'success','message' => '删除成功']);
    }

    public function setWebSiteInfo(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $title       = $request->get('title');
        $desc        = $request->get('desc');
        $keyword     = $request->get('keyword');
        $url         = $request->get('url');
        $footerInfo  = $request->get('footer_info');
        $record      = $request->get('record');
        $logoId      = !is_null($request->get('logo_id')) ? $request->get('logo_id') : 0;

        if(!is_null($title)){
            $thisTitle = SysInfo::whereType('title')->first();
            if(!$thisTitle){
                $thisTitle = new SysInfo();
                $thisTitle->type  = 'title';
            }

            $thisTitle->content   = $title;
            $thisTitle->save();
        }
        if(!is_null($desc)){
            $thisTitle = SysInfo::whereType('desc')->first();
            if(!$thisTitle){
                $thisTitle = new SysInfo();
                $thisTitle->type  = 'desc';
            }

            $thisTitle->content   = $desc;
            $thisTitle->save();
        }
        if(!is_null($keyword)){
            $thisTitle = SysInfo::whereType('keyword')->first();
            if(!$thisTitle){
                $thisTitle = new SysInfo();
                $thisTitle->type  = 'keyword';
            }

            $thisTitle->content   = $keyword;
            $thisTitle->save();
        }

        if(!is_null($url)){
            $thisTitle = SysInfo::whereType('url')->first();
            if(!$thisTitle){
                $thisTitle = new SysInfo();
                $thisTitle->type  = 'url';
            }

            $thisTitle->content   = $url;
            $thisTitle->save();
        }

        if(!is_null($footerInfo)){
            $thisTitle = SysInfo::whereType('footer')->first();
            if(!$thisTitle){
                $thisTitle = new SysInfo();
                $thisTitle->type  = 'footer';
            }

            $thisTitle->content   = $footerInfo;
            $thisTitle->save();
        }
        if(!is_null($record)){
            $thisTitle = SysInfo::whereType('record')->first();
            if(!$thisTitle){
                $thisTitle = new SysInfo();
                $thisTitle->type  = 'record';
            }

            $thisTitle->content   = $record;
            $thisTitle->save();
        }

        if($logoId != 0){
            $thisTitle = SysInfo::whereType('logo')->first();
            if(!$thisTitle){
                $thisTitle = new SysInfo();
                $thisTitle->type  = 'logo';
            }

            $thisTitle->content   = $logoId;
            $thisTitle->save();
        }

        return response()->json(['level' => 'success','message' => '信息设置成功']);
    }

    public function addFooterMenu(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $id   = !is_null($request->get('id')) ? $request->get('id') : 0;
        $pid  = !is_null($request->get('pid')) ? $request->get('pid') : 0;
        $name = $request->get('name');
        $sort = $request->get('sort');
        $link = $request->get('link');

        $newFooterMenu = FooterMenu::whereId($id)->first();
        if(!$newFooterMenu)
            $newFooterMenu = new FooterMenu();

        $newFooterMenu->pid                     = $pid;
        if(!is_null($name))$newFooterMenu->name = $name;
        if(!is_null($link))$newFooterMenu->link = $link;
        $newFooterMenu->sort                    = $sort;
        $newFooterMenu->save();

        return response()->json(['level' => 'success','message' => '新增成功','data' => $newFooterMenu]);
    }

    public function delFooterMenu(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(["level"=>"error","message"=>"请先登录！"]);

        $id = $request->get("id");

        /** @var FooterMenu $theMenu */
        $theMenu = FooterMenu::whereId($id)->first();
        if(!$theMenu)
            return response()->json(["level"=>"error","message"=>"当前栏目不存在"]);

        $theMenu->delete();

        return response()->json(["level"=>"success","message"=>"信息设置成功"]);

    }

    public function setGoods(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $name                      = $request->get('name');
        $desc                      = $request->get('desc');
        $imgId                     = $request->get('thumb_id');
        $thumbImgId                = $request->get('thumb_img_id');
        $content                   = $request->get('content');
        $type                      = !is_null($request->get('type')) ? $request->get('type') : '';
        $classifyId                = $request->get('classify_id');
        $id                        = !is_null($request->get('id'))?$request->get('id'):0;
        $sort                      = !is_null($request->get('sort'))?$request->get('sort'):0;
        $classifyName   = !is_null($request->get('classify_name')) ? $request->get('classify_name') : '';
        $classifyDesc   = !is_null($request->get('classify_desc')) ? $request->get('classify_desc') : '';

        if($type == 'philanthropy'){
            $newAllGoods = Philanthropy::whereId($id)->first();
            if(!$newAllGoods)
                $newAllGoods = new Philanthropy();

            if(!is_null($name))$newAllGoods->title                     = $name;
            if(!is_null($desc))$newAllGoods->desc                      = $desc;
            if(!is_null($imgId))$newAllGoods->img_id                   = $imgId;
            if(!is_null($thumbImgId))$newAllGoods->thumb_img_id        = $thumbImgId;
            if(!is_null($content))$newAllGoods->content                = $content;
            $newAllGoods->sort                                         = $sort;
            $newAllGoods->save();
        }elseif($type == 'en_philanthropy'){
            $newAllGoods = EnPhilanthropy::whereId($id)->first();
            if(!$newAllGoods)
                $newAllGoods = new EnPhilanthropy();

            if(!is_null($name))$newAllGoods->title                     = $name;
            if(!is_null($desc))$newAllGoods->desc                      = $desc;
            if(!is_null($imgId))$newAllGoods->img_id                   = $imgId;
            if(!is_null($thumbImgId))$newAllGoods->thumb_img_id        = $thumbImgId;
            if(!is_null($content))$newAllGoods->content                = $content;
            $newAllGoods->sort                                         = $sort;
            $newAllGoods->save();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        return response()->json(['level' => 'success','message' => '添加或修改成功',"data"=>$newAllGoods]);
    }

    public function delGoods(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id   = !is_null($request->get('id'))?$request->get('id'):0;
        $type = !is_null($request->get('type')) ? $request->get('type') : '';

        if($type == 'philanthropy'){
            $thatGoods = Philanthropy::whereId($id)->first();
        }elseif($type == 'en_philanthropy'){
            $thatGoods = EnPhilanthropy::whereId($id)->first();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误！']);
        }

        if(!$thatGoods)
            return response()->json(["level"=>"error","message"=>"该数据不存在！"]);

        $thatGoods->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatGoods]);
    }

    public function addAllClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $name = $request->get("name");
        $sort = $request->get('sort');
        $type = $request->get('type');
        $id   = !is_null($request->get('id'))?$request->get('id'):0;

        $newClassify = Classify::whereId(trim($id))->first();
        if(!$newClassify){
            $newClassify = new Classify();
            if(!is_null($type))$newClassify->type = $type;
        }


        if(!is_null($name))$newClassify->name = $name;
        if(!is_null($sort))$newClassify->sort = $sort;
        $newClassify->save();

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newClassify]);
    }

    public function delAllClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $thatClassify =Classify::whereId($id)->first();

        if(!$thatClassify)
            return response()->json(["level"=>"error","message"=>"该分类不存在！"]);

        $thatClassify->delete();

        return response()->json(['level' => 'success','message' => '删除成功']);
    }

    public function setNews(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title        = $request->get("title");
        $desc         = $request->get('desc');
        $imgId   = !is_null($request->get('img_id')) ? $request->get('img_id') : 0;
        $thumbImgId   = !is_null($request->get('thumb_img_id')) ? $request->get('thumb_img_id') : 0;
        $content      = $request->get('content');
        $type         = !is_null($request->get('type')) ? $request->get('type') : '';
        $id           = !is_null($request->get('id')) ? $request->get('id') : 0;
        $sort         = !is_null($request->get('sort')) ? $request->get('sort') : 0;
        $classifyName   = !is_null($request->get('classify_name')) ? $request->get('classify_name') : '';
        $classifyDesc   = !is_null($request->get('classify_desc')) ? $request->get('classify_desc') : '';

        if($type == 'news'){
            $newAllNews = \App\News::whereId($id)->first();
            if(!$newAllNews)
                $newAllNews = new \App\News();

            if(!is_null($title))$newAllNews->title                 = $title;
            if(!is_null($desc))$newAllNews->desc                   = $desc;
            $newAllNews->img_id                                    = $imgId;
            $newAllNews->thumb_img_id                              = $thumbImgId;
            $newAllNews->sort                                      = $sort;
            if(!is_null($content))$newAllNews->content             = $content;
            $newAllNews->save();

        }elseif($type == 'en_news'){
            $newAllNews = EnNews::whereId($id)->first();
            if(!$newAllNews)
                $newAllNews = new EnNews();

            if(!is_null($title))$newAllNews->title                 = $title;
            if(!is_null($desc))$newAllNews->desc                   = $desc;
            $newAllNews->img_id                                    = $imgId;
            $newAllNews->thumb_img_id                              = $thumbImgId;;
            $newAllNews->sort                                      = $sort;
            if(!is_null($content))$newAllNews->content             = $content;
            $newAllNews->save();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newAllNews]);
    }

    public function delNews(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $type = !is_null($request->get('type')) ? $request->get('type') : '';

        if($type == 'news'){
            $thatNews =\App\News::whereId($id)->first();
        }elseif($type == 'en_news'){
            $thatNews = EnNews::whereId($id)->first();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误!']);
        }

        if(!$thatNews)
            return response()->json(["level"=>"error","message"=>"该新闻不存在！"]);

        $thatNews->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatNews]);
    }

    public function setProjectCase(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title        = $request->get("title");
        $desc         = $request->get('desc');
        $imgId   = !is_null($request->get('img_id')) ? $request->get('img_id') : 0;
        $thumbImgId   = !is_null($request->get('thumb_img_id')) ? $request->get('thumb_img_id') : 0;
        $content      = $request->get('content');
        $type         = !is_null($request->get('type')) ? $request->get('type') : '';
        $id           = !is_null($request->get('id')) ? $request->get('id') : 0;
        $sort         = !is_null($request->get('sort')) ? $request->get('sort') : 0;
        $classifyName   = !is_null($request->get('classify_name')) ? $request->get('classify_name') : '';
        $classifyDesc   = !is_null($request->get('classify_desc')) ? $request->get('classify_desc') : '';

        if($type == 'project_case'){
            $newAllNews = ProjectCases::whereId($id)->first();
            if(!$newAllNews)
                $newAllNews = new ProjectCases();

            if(!is_null($title))$newAllNews->title                 = $title;
            if(!is_null($desc))$newAllNews->desc                   = $desc;
            $newAllNews->img_id                                    = $imgId;
            $newAllNews->thumb_img_id                              = $thumbImgId;
            $newAllNews->sort                                      = $sort;
            if(!is_null($content))$newAllNews->content             = $content;
            $newAllNews->save();
        }elseif($type == 'en_project_case'){
            $newAllNews = EnProjectCases::whereId($id)->first();
            if(!$newAllNews)
                $newAllNews = new EnProjectCases();

            if(!is_null($title))$newAllNews->title                 = $title;
            if(!is_null($desc))$newAllNews->desc                   = $desc;
            $newAllNews->img_id                                    = $imgId;
            $newAllNews->thumb_img_id                              = $thumbImgId;
            $newAllNews->sort                                      = $sort;
            if(!is_null($content))$newAllNews->content             = $content;
            $newAllNews->save();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newAllNews]);
    }

    public function delProjectCase(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $type = !is_null($request->get('type')) ? $request->get('type') : '';

        if($type == 'project_case'){
            $thatNews =ProjectCases::whereId($id)->first();
        }elseif($type == 'en_project_case'){
            $thatNews = EnProjectCases::whereId($id)->first();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误!']);
        }

        if(!$thatNews)
            return response()->json(["level"=>"error","message"=>"该新闻不存在！"]);

        $thatNews->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatNews]);
    }

    public function setCharitable(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title        = $request->get("title");
        $desc         = $request->get('desc');
        $imgId        = !is_null($request->get('img_id')) ? $request->get('img_id') : 0;
        $thumbImgId   = !is_null($request->get('thumb_img_id')) ? $request->get('thumb_img_id') : 0;
        $content      = $request->get('content');
        $type         = !is_null($request->get('type')) ? $request->get('type') : '';
        $id           = !is_null($request->get('id')) ? $request->get('id') : 0;
        $sort         = !is_null($request->get('sort')) ? $request->get('sort') : 0;
        $classifyName   = !is_null($request->get('classify_name')) ? $request->get('classify_name') : '';
        $classifyDesc   = !is_null($request->get('classify_desc')) ? $request->get('classify_desc') : '';

        if($type == 'charitable'){
            $newAllNews = CharitableActivities::whereId($id)->first();
            if(!$newAllNews)
                $newAllNews = new CharitableActivities();

            if(!is_null($title))$newAllNews->title                 = $title;
            if(!is_null($desc))$newAllNews->desc                   = $desc;
            $newAllNews->img_id                                    = $imgId;
            $newAllNews->thumb_img_id                              = $thumbImgId;
            $newAllNews->sort                                      = $sort;
            if(!is_null($content))$newAllNews->content             = $content;
            $newAllNews->save();
        }elseif($type == 'en_charitable'){
            $newAllNews = EnCharitableActivities::whereId($id)->first();
            if(!$newAllNews)
                $newAllNews = new EnCharitableActivities();

            if(!is_null($title))$newAllNews->title                 = $title;
            if(!is_null($desc))$newAllNews->desc                   = $desc;
            $newAllNews->img_id                                    = $imgId;
            $newAllNews->thumb_img_id                              = $thumbImgId;
            $newAllNews->sort                                      = $sort;
            if(!is_null($content))$newAllNews->content             = $content;
            $newAllNews->save();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newAllNews]);
    }

    public function delCharitable(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $type = !is_null($request->get('type')) ? $request->get('type') : '';

        if($type == 'charitable'){
            $thatNews =CharitableActivities::whereId($id)->first();
        }elseif($type == 'en_charitable'){
            $thatNews = EnCharitableActivities::whereId($id)->first();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误!']);
        }

        if(!$thatNews)
            return response()->json(["level"=>"error","message"=>"该新闻不存在！"]);

        $thatNews->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatNews]);
    }

    public function setEnvironmental(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title        = $request->get("title");
        $desc         = $request->get('desc');
        $imgId        = !is_null($request->get('img_id')) ? $request->get('img_id') : 0;
        $thumbImgId   = !is_null($request->get('thumb_img_id')) ? $request->get('thumb_img_id') : 0;
        $classifyId   = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        $content      = $request->get('content');
        $type         = !is_null($request->get('type')) ? $request->get('type') : '';
        $id           = !is_null($request->get('id')) ? $request->get('id') : 0;
        $sort         = !is_null($request->get('sort')) ? $request->get('sort') : 0;
        $classifyName   = !is_null($request->get('classify_name')) ? $request->get('classify_name') : '';
        $classifyDesc   = !is_null($request->get('classify_desc')) ? $request->get('classify_desc') : '';

        if($type == 'environmental'){
            if($classifyId == '' || $classifyId == 0)
                return response()->json(['level' => 'error','message' => '请选择分类']);

            $newAllNews = EnvironmentalProtectionKnowledge::whereId($id)->first();
            if(!$newAllNews)
                $newAllNews = new EnvironmentalProtectionKnowledge();

            if(!is_null($title))$newAllNews->title                 = $title;
            if(!is_null($desc))$newAllNews->desc                   = $desc;
            $newAllNews->img_id                                    = $imgId;
            $newAllNews->thumb_img_id                              = $thumbImgId;
            $newAllNews->sort                                      = $sort;
            if(!is_null($content))$newAllNews->content             = $content;
            if(!is_null($classifyId))$newAllNews->classify_two_id  = $classifyId;
            $newAllNews->save();
        }elseif($type == 'en_environmental'){
            if($classifyId == '' || $classifyId == 0)
                return response()->json(['level' => 'error','message' => '请选择分类']);

            $newAllNews = EnEnvironmentalProtectionKnowledge::whereId($id)->first();
            if(!$newAllNews)
                $newAllNews = new EnEnvironmentalProtectionKnowledge();

            if(!is_null($title))$newAllNews->title                 = $title;
            if(!is_null($desc))$newAllNews->desc                   = $desc;
            $newAllNews->img_id                                    = $imgId;
            $newAllNews->thumb_img_id                              = $thumbImgId;
            $newAllNews->sort                                      = $sort;
            if(!is_null($content))$newAllNews->content             = $content;
            if(!is_null($classifyId))$newAllNews->classify_two_id  = $classifyId;
            $newAllNews->save();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newAllNews]);
    }

    public function delEnvironmental(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $type = !is_null($request->get('type')) ? $request->get('type') : '';

        if($type == 'environmental'){
            $thatNews =EnvironmentalProtectionKnowledge::whereId($id)->first();
        }elseif($type == 'en_environmental'){
            $thatNews = EnEnvironmentalProtectionKnowledge::whereId($id)->first();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误!']);
        }

        if(!$thatNews)
            return response()->json(["level"=>"error","message"=>"该新闻不存在！"]);

        $thatNews->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatNews]);
    }

    public function setAboutUs(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $content      = $request->get('content');
        $title        = !is_null($request->get('title')) ? $request->get('title') : 0;
        $type         = !is_null($request->get('type')) ? $request->get('type') : '';
        $imgId        = !is_null($request->get('img_id')) ? $request->get('img_id') : 0;
        $thumbImgId   = !is_null($request->get('thumb_img_id')) ? $request->get('thumb_img_id') : 0;
        $id           = !is_null($request->get('id')) ? $request->get('id') : 0;
        $sort         = !is_null($request->get('sort')) ? $request->get('sort') : 0;

        if($type == 'about_us'){
            $newAllAboutUs = AboutUs::whereId($id)->first();

            if(!$newAllAboutUs){
                $newAllAboutUs = new AboutUs();
            }

            if(!is_null($content))$newAllAboutUs->content             = $content;
            $newAllAboutUs->title                                     = $title;
            $newAllAboutUs->img_id                                    = $imgId;
            $newAllAboutUs->thumb_img_id                              = $thumbImgId;
            $newAllAboutUs->sort                                      = $sort;
            $newAllAboutUs->save();
        }elseif($type == 'en_about_us'){
            $newAllAboutUs = EnAboutUs::whereId($id)->first();
            if(!$newAllAboutUs){
                $newAllAboutUs = new EnAboutUs();
            }

            if(!is_null($content))$newAllAboutUs->content             = $content;
            $newAllAboutUs->title                                     = $title;
            $newAllAboutUs->img_id                                    = $imgId;
            $newAllAboutUs->thumb_img_id                              = $thumbImgId;
            $newAllAboutUs->sort                                      = $sort;
            $newAllAboutUs->save();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }


        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newAllAboutUs]);
    }

    public function delAboutUs(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id   = !is_null($request->get('id'))?$request->get('id'):0;
        $type = !is_null($request->get('type')) ? $request->get('type') : '';

        if($type == 'about_us'){
            $thatAboutUs = AboutUs::whereId($id)->first();
        }elseif($type == 'en_about_us'){
            $thatAboutUs = EnAboutUs::whereId($id)->first();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        if(!$thatAboutUs)
            return response()->json(["level"=>"error","message"=>"该信息不存在！"]);

        $thatAboutUs->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatAboutUs]);
    }

    public function setHumanRes(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $content      = $request->get('content');
        $realname     = !is_null($request->get('realname')) ? $request->get('realname') : '';
        $position     = !is_null($request->get('position')) ? $request->get('position') : 0;
        $imgId        = !is_null($request->get('img_id')) ? $request->get('img_id') : 0;
        $type         = !is_null($request->get('type')) ? $request->get('type') : '';
        $id           = !is_null($request->get('id')) ? $request->get('id') : 0;
        $sort         = !is_null($request->get('sort')) ? $request->get('sort') : 0;
        $classifyName   = !is_null($request->get('classify_name')) ? $request->get('classify_name') : '';
        $classifyDesc   = !is_null($request->get('classify_desc')) ? $request->get('classify_desc') : '';

        if($type == 'comment'){
            $newAllHumanRes = Comment::whereId($id)->first();

            if(!$newAllHumanRes)
                $newAllHumanRes = new Comment();

            if(!is_null($content))$newAllHumanRes->content              = $content;
            $newAllHumanRes->realname                                   = $realname;
            $newAllHumanRes->position                                   = $position;
            $newAllHumanRes->photo_id                                   = $imgId;
            $newAllHumanRes->sort                                       = $sort;
            $newAllHumanRes->save();
        }elseif($type == 'en_comment'){
            $newAllHumanRes = EnComment::whereId($id)->first();
            if(!$newAllHumanRes)
                $newAllHumanRes = new EnComment();

            if(!is_null($content))$newAllHumanRes->content              = $content;
            $newAllHumanRes->realname                                   = $realname;
            $newAllHumanRes->position                                   = $position;
            $newAllHumanRes->photo_id                                   = $imgId;
            $newAllHumanRes->sort                                       = $sort;
            $newAllHumanRes->save();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }


        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newAllHumanRes]);
    }

    public function delHumanRes(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $type         = !is_null($request->get('type')) ? $request->get('type') : '';

        if($type == 'comment'){
            $thatHumanRes = Comment::whereId($id)->first();
        }elseif($type == 'en_comment'){
            $thatHumanRes = EnComment::whereId($id)->first();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        if(!$thatHumanRes)
            return response()->json(["level"=>"error","message"=>"该信息不存在！"]);

        $thatHumanRes->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatHumanRes]);
    }

    public function setEnWebSiteInfo(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $title       = $request->get('title');
        $desc        = $request->get('desc');
        $keyword     = $request->get('keyword');
        $url         = $request->get('url');
        $footerInfo  = $request->get('footer_info');
        $record      = $request->get('record');
        $logoId      = !is_null($request->get('logo_id')) ? $request->get('logo_id') : 0;

        if(!is_null($title)){
            $thisTitle = EnSysInfo::whereType('title')->first();
            if(!$thisTitle){
                $thisTitle = new EnSysInfo();
                $thisTitle->type  = 'title';
            }

            $thisTitle->content   = $title;
            $thisTitle->save();
        }
        if(!is_null($desc)){
            $thisTitle = EnSysInfo::whereType('desc')->first();
            if(!$thisTitle){
                $thisTitle = new EnSysInfo();
                $thisTitle->type  = 'desc';
            }

            $thisTitle->content   = $desc;
            $thisTitle->save();
        }
        if(!is_null($keyword)){
            $thisTitle = EnSysInfo::whereType('keyword')->first();
            if(!$thisTitle){
                $thisTitle = new EnSysInfo();
                $thisTitle->type  = 'keyword';
            }

            $thisTitle->content   = $keyword;
            $thisTitle->save();
        }

        if(!is_null($url)){
            $thisTitle = EnSysInfo::whereType('url')->first();
            if(!$thisTitle){
                $thisTitle = new EnSysInfo();
                $thisTitle->type  = 'url';
            }

            $thisTitle->content   = $url;
            $thisTitle->save();
        }

        if(!is_null($footerInfo)){
            $thisTitle = EnSysInfo::whereType('footer')->first();
            if(!$thisTitle){
                $thisTitle = new EnSysInfo();
                $thisTitle->type  = 'footer';
            }

            $thisTitle->content   = $footerInfo;
            $thisTitle->save();
        }
        if(!is_null($record)){
            $thisTitle = EnSysInfo::whereType('record')->first();
            if(!$thisTitle){
                $thisTitle = new EnSysInfo();
                $thisTitle->type  = 'record';
            }

            $thisTitle->content   = $record;
            $thisTitle->save();
        }

        if($logoId != 0){
            $thisTitle = EnSysInfo::whereType('logo')->first();
            if(!$thisTitle){
                $thisTitle = new EnSysInfo();
                $thisTitle->type  = 'logo';
            }

            $thisTitle->content   = $logoId;
            $thisTitle->save();
        }

        return response()->json(['level' => 'success','message' => '信息设置成功']);
    }

    public function addEnFooterMenu(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $id   = !is_null($request->get('id')) ? $request->get('id') : 0;
        $pid  = !is_null($request->get('pid')) ? $request->get('pid') : 0;
        $name = $request->get('name');
        $sort = $request->get('sort');
        $link = $request->get('link');

        $newFooterMenu = EnFooterMenu::whereId($id)->first();
        if(!$newFooterMenu)
            $newFooterMenu = new EnFooterMenu();

        $newFooterMenu->pid                     = $pid;
        if(!is_null($name))$newFooterMenu->name = $name;
        if(!is_null($link))$newFooterMenu->link = $link;
        $newFooterMenu->sort                    = $sort;
        $newFooterMenu->save();

        return response()->json(['level' => 'success','message' => '新增成功','data' => $newFooterMenu]);
    }

    public function delEnFooterMenu(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(["level"=>"error","message"=>"请先登录！"]);

        $id = $request->get("id");

        /** @var EnFooterMenu $theEnMenu */
        $theEnMenu = EnFooterMenu::whereId($id)->first();
        if(!$theEnMenu)
            return response()->json(["level"=>"error","message"=>"当前栏目不存在"]);

        $theEnMenu->delete();

        return response()->json(["level"=>"success","message"=>"信息设置成功"]);

    }

    public function setApplicationField(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(["level"=>"error","message"=>"请先登录！"]);

        $id             = !is_null($request->get('id')) ? $request->get('id') : 0;
        $sort           = !is_null($request->get('sort')) ? $request->get('sort') : 0;
        $name           = $request->get('name');
        $imgId          = !is_null($request->get('img_id')) ? $request->get('img_id') : 0;
        $numType        = $request->get('num_type');
        $num            = $request->get('num');
        $type           = $request->get('type');
        $classifyName   = !is_null($request->get('classify_name')) ? $request->get('classify_name') : '';
        $classifyDesc   = !is_null($request->get('classify_desc')) ? $request->get('classify_desc') : '';

        if(!is_null($type) && $type == 'achievement'){
            $newApplicationField = Achievement::whereId($id)->first();
            if(!$newApplicationField){
                $isApplicationField = Achievement::all();
                if(count($isApplicationField) > 4)
                    return response()->json(['level' => 'error','message' => '只能新增三条']);

                $newApplicationField = new Achievement();
            }


            $newApplicationField->img_id                            = $imgId;
            if(!is_null($name))$newApplicationField->title          = $name;
            if(!is_null($numType))$newApplicationField->num_type    = $numType;
            if(!is_null($num))$newApplicationField->num             = $num;
            $newApplicationField->sort                               = $sort;
            $newApplicationField->save();
        }elseif(!is_null($type) && $type == 'en_achievement'){
            $newApplicationField = EnAchievement::whereId($id)->first();
            if(!$newApplicationField){
                $isApplicationField = EnAchievement::all();
                if(count($isApplicationField) > 4)
                    return response()->json(['level' => 'error','message' => '只能新增三条']);

                $newApplicationField = new EnAchievement();
            }


            $newApplicationField->img_id                           = $imgId;
            if(!is_null($name))$newApplicationField->title          = $name;
            if(!is_null($numType))$newApplicationField->num_type    = $numType;
            if(!is_null($num))$newApplicationField->num             = $num;
            $newApplicationField->sort                               = $sort;
            $newApplicationField->save();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newApplicationField]);
    }

    public function delApplicationField(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(["level"=>"error","message"=>"请先登录！"]);

        $id = $request->get("id");
        $type = $request->get('type');

        if(!is_null($type) && $type == 'achievement') {
            /** @var Achievement $thatApplicationField */
            $thatApplicationField = Achievement::whereId($id)->first();
        }elseif(!is_null($type) && $type == 'en_achievement'){
            /** @var Achievement $thatApplicationField */
            $thatApplicationField = EnAchievement::whereId($id)->first();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        if(!$thatApplicationField)
            return response()->json(["level"=>"error","message"=>"当前信息不存在"]);

        $thatApplicationField->delete();

        return response()->json(["level"=>"success","message"=>"删除成功",'data' => $thatApplicationField]);
    }

    public function addOnline(Request $request){
        $name    = $request->get('name') ? $request->get('name') : '';
        $email   = $request->get('email') ? $request->get('email') : '';
        $subject = $request->get('subject') ? $request->get('subject') : '';
        $content = $request->get('msg') ? $request->get('msg') : '';
        $type        = $request->get('type');

        if(is_null($type) || $type == '')
            return response()->json(['level' => 'error','message' => 'system error']);

        if(is_null($name) || $name == ''){
            if($type == 'zh'){
                return response()->json(['level' => 'error','message' => '请填写姓名']);
            }elseif($type == 'en'){
                return response()->json(['level' => 'error','message' => 'Please fill in the name']);
            }else{
                return response()->json(['level' => 'error','message' => 'system error']);
            }

        }

        if(is_null($subject) || $subject == ''){
            if($type == 'zh'){
                return response()->json(['level' => 'error','message' => '请填写学科']);
            }elseif($type == 'en'){
                return response()->json(['level' => 'error','message' => 'Please fill in the subject']);
            }else{
                return response()->json(['level' => 'error','message' => 'system error']);
            }
        }

        if(is_null($email) || $email == ''){
            if($type == 'zh'){
                return response()->json(['level' => 'error','message' => '请填写联系邮箱']);
            }elseif($type == 'en'){
                return response()->json(['level' => 'error','message' => 'Please fill in the contact email']);
            }else{
                return response()->json(['level' => 'error','message' => 'system error']);
            }
        }

        if(!preg_match("/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/",$email)){
            if($type == 'zh'){
                return response()->json(['level' => 'error','message' => '邮箱格式不正确']);
            }elseif($type == 'en'){
                return response()->json(['level' => 'error','message' => 'E-mail format is incorrect']);
            }else{
                return response()->json(['level' => 'error','message' => 'system error']);
            }
        }

        if(is_null($content) || $content == ''){
            if($type == 'zh'){
                return response()->json(['level' => 'error','message' => '请填写留言内容']);
            }elseif($type == 'en'){
                return response()->json(['level' => 'error','message' => 'Please fill in the message']);
            }else{
                return response()->json(['level' => 'error','message' => 'system error']);
            }
        }

        $newOnlineMessage = new OnlineMessage();
        $newOnlineMessage->realname          = $name;
        $newOnlineMessage->subject           = $subject;
        $newOnlineMessage->e_mail            = $email;
        $newOnlineMessage->content           = $content;
        $newOnlineMessage->save();

        return response()->json(['level' => 'success','message' => '留言成功，请等待回复','data' => $newOnlineMessage]);
    }

    public function changeSectionNav(Request $request){
        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $classify = Classify::whereId($id)->first();
        if(!$classify)
            return response()->json(['leevl' => 'error','message' => '该数据不存在']);

        if($classify->is_nav == 1){
            $classify->is_nav = 0;
        }else{
            $classify->is_nav = 1;
        }

        $classify->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $classify]);
    }

    public function changeSectionStatus(Request $request){
        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $classify = Classify::whereId($id)->first();
        if(!$classify)
            return response()->json(['leevl' => 'error','message' => '该数据不存在']);

        if($classify->status == 1){
            $classify->status = 0;
        }else{
            $classify->status = 1;
        }

        $classify->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $classify]);
    }
}